﻿using Microsoft.WowAddonStudio.Lua.Model;

using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.Tests.Model
{
    [TestFixture]
    public class QualifiedNameTest
    {
        [Test]
        public void QualifyNameTest()
        {
            var qualifiedName = new QualifiedName("x");

            Assert.AreEqual("x", qualifiedName.FirstIdentifier.Name);
            Assert.AreEqual("x", qualifiedName.ToString());

            qualifiedName = qualifiedName.Qualify("y");

            Assert.AreEqual("x", qualifiedName.FirstIdentifier.Name);
            Assert.AreEqual("x.y", qualifiedName.ToString());
        }

        [Test]
        public void QualifyMethodTest()
        {
            var qualifiedMethodName = new QualifiedName("foo").QualifyMethod("bar");

            Assert.IsTrue(qualifiedMethodName.IsMethod);
            Assert.AreEqual("bar", qualifiedMethodName.MethodIdentifier.Name);
            Assert.AreEqual("foo:bar", qualifiedMethodName.ToString());
        }

        [Test]
        public void ImplicitConversionTest()
        {
            QualifiedName qualifiedName = "foo.bar.x";

            Assert.AreEqual("foo", qualifiedName.FirstIdentifier.Name);
            Assert.AreEqual("foo.bar.x", qualifiedName.ToString());

            QualifiedName qualifiedMethodName = "foo.bar:x";

            Assert.AreEqual("foo", qualifiedMethodName.FirstIdentifier.Name);
            Assert.AreEqual("x", qualifiedMethodName.MethodIdentifier.Name);
            Assert.AreEqual("foo.bar:x", qualifiedMethodName.ToString());

        }

        [Test]
        public void UnqualifyTest()
        {
            QualifiedName fooBar = "foo.bar";

            QualifiedName bar = fooBar.Unqualify();
            Assert.AreEqual(bar, (QualifiedName)"bar");
        }

        [Test]
        public void UnqualifyMethodTest()
        {
            QualifiedName fooBar = "foo:bar";

            Assert.IsTrue(fooBar.IsMethod);
            Assert.IsFalse(fooBar.Unqualify(true).IsMethod);
            Assert.AreEqual("foo", fooBar.Unqualify(true).ToString());
        }

        [Test]
        public void OperatorTest()
        {
            QualifiedName fooBar = "foo.bar";

            Assert.IsTrue(fooBar == new QualifiedName("foo.bar"));
            Assert.IsFalse(fooBar != new QualifiedName("foo.bar"));

            Assert.IsFalse(fooBar == new QualifiedName("x.y"));
            Assert.IsTrue(fooBar != new QualifiedName("x.y"));
        }

        [Test]
        public void NullValueTest()
        {
            QualifiedName fooBar = null;

            Assert.IsTrue(fooBar == null);
            Assert.IsFalse(fooBar != null);

            fooBar = "foo.bar";

            Assert.IsFalse(fooBar == null);
            Assert.IsTrue(fooBar != null);
        }
    }
}
